﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hr20.DAL;
using Hr20.BLL;
using MyGeneration.dOOdads;
using MethodIT.Helper;

namespace Appraisal.Web.ZB
{
    public partial class SubjectResult : System.Web.UI.Page
    {
        string sStyleCenter = "color: windowtext;text-decoration: none;text-align: justify;vertical-align: middle;white-space: normal;border: .5pt solid windowtext;padding: 0px;text-align: center;";
        string sStyleLeft = "color: windowtext;text-decoration: none;text-align: justify;vertical-align: middle;white-space: normal;border: .5pt solid windowtext;padding: 0px;text-align: left;";
        string sStyleRight = "color: windowtext;text-decoration: none;text-align: justify;vertical-align: middle;white-space: normal;border: .5pt solid windowtext;padding: 0px;text-align: right;";
        string sRIGHT = "font-size: 11.0pt;font-weight: 400;font-style: normal;text-decoration: none;font-family: 宋体;text-align: right;";
        string sLEFT = "font-size: 11.0pt;font-weight: 400;font-style: normal;text-decoration: none;font-family: 宋体;text-align: left;";
        string sCENTER = "font-size: 11.0pt;font-weight: 400;font-style: normal;text-decoration: none;font-family: 宋体;text-align: center;";

        protected void Page_Load(object sender, EventArgs e)
        {
            btnPrint.Attributes.Add("onclick", "excel(p1);");
            if (!Page.IsPostBack)
            {
                int SubID = HelperURL.GetIntFromUrlString("Subid");
                int TypeID = HelperURL.GetIntFromUrlString("Typeid");
                if (SubID != -1) LoadData(SubID);
                if (TypeID ==-1) 
                {
                    CreateSubject(SubID);
                }
                else
                {
                    CreateResult(SubID);
                }
            }
        }

        private void LoadData(int SubID)
        {
            vw_Subject_Tender se = new vw_Subject_Tender();
            se.Where.SubID.Value = SubID;
            se.Query.AddOrderBy(vw_Subject_Tender.ColumnNames.OrderId , WhereParameter.Dir.ASC);
            se.Query.Load();
            labSubjectName.Text = se.SubName;
            FunctionSet.FillListControl(dplTender, se.DefaultView.Table, "TenderName", "TenderID");

            ZB_Result result = new ZB_Result();
            result.Where.SubjectID.Value = SubID;
            result.Query.Load();
            while (!result.EOF)
            {
                ZB_Tender tender = new ZB_Tender();
                tender.LoadByPrimaryKey(result.TenderID);
                labResult.Text += tender.FullName + "，";

                result.MoveNext();
            }
            labResult.Text = labResult.Text.TrimEnd('，');
            labResult.Text += "。";
        }

        private void CreateSubject(int SubID)
        {
            KP_Category cate = new KP_Category();


            Table tb = new Table();
            tb.Attributes.Add("border","0");
            tb.Attributes.Add("width", "100%");
            tb.Attributes.Add("cellpadding","0");
            tb.Attributes.Add("cellspacing","0");
            tb.Attributes.Add("style","border-collapse:collapse");

            TableRow tr = new TableRow();
            TableCell tc = new TableCell();

            vw_Subject_Tender se = new vw_Subject_Tender();
            se.Where.SubID.Value = SubID;
            se.Query.AddOrderBy(vw_Subject_Tender.ColumnNames.Grade1, WhereParameter.Dir.ASC);
            se.Query.Load();
            int TenderCount = se.RowCount;

            tc = HelperTable.CreateStyleCell(1, 2 + TenderCount, "评 标 报 告", "font-size: 18.0pt;font-weight: 700;font-style: normal;text-decoration: none;font-family: 黑体;text-align: center;vertical-align: middle;white-space: normal;");
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);

            KP_Subject subject = new KP_Subject();
            subject.LoadByPrimaryKey(SubID);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1,1,"项目名称：",sStyleCenter));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1 + TenderCount, subject.SubjectName, sStyleLeft));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "项目编号：", sStyleCenter));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1 + TenderCount, subject.Code, sStyleLeft));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "评标时间：", sStyleCenter));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1 + TenderCount, DateTime.Now.ToShortDateString(), sStyleLeft));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "评标地点：", sStyleCenter));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1 + TenderCount, subject.Address, sStyleLeft));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "主持人：", sStyleCenter));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1 + TenderCount, "", sStyleLeft));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "监标人：", sStyleCenter));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1 + TenderCount, "", sStyleLeft));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 2 + TenderCount, "评标因素一览表", sStyleCenter));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "投标人", sStyleCenter));
            se.Rewind();
            while (!se.EOF)
            {
                tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, se.TenderName, sStyleCenter));
                se.MoveNext();
            }
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "权重", sStyleCenter));
            tb.Rows.Add(tr);

            vw_Subject_Table kpTable = new vw_Subject_Table();
            kpTable.Where.SubjectID.Value = SubID;
            kpTable.Where.KpType.Value = 3;//只看价格表
            //kpTable.Where.KpType.Operator = WhereParameter.Operand.NotEqual ;
            kpTable.Query.Load();
            VW_ZB_ItemResult priceItem = new VW_ZB_ItemResult();
            priceItem.Where.SubID.Value = SubID;
            priceItem.Where.KpID.Value = kpTable.KpID;
            priceItem.Where.Is_Group.Value = false;
            priceItem.Where.Item_Type.Value = 1;
            priceItem.Query.Load();
            tr = new TableRow();
            //这里显示报价
            //tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, priceItem.KpName, sStyleCenter));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "投标报价", sStyleCenter));

            //显示价格指标应答
            se.Rewind();
            while (!se.EOF)
            {
                DataRow[] drs = priceItem.DefaultView.Table.Select("TenderID=" + se.TenderID);
                tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, drs[0][VW_ZB_ItemResult.ColumnNames.ItemText].ToString(), sStyleCenter));
                se.MoveNext();
            }
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "", sStyleCenter));
            tb.Rows.Add(tr);

            kpTable = new vw_Subject_Table();
            kpTable.Where.SubjectID.Value = SubID;
            kpTable.Where.KpType.Value = 4;//不显示应答评分表
            kpTable.Where.KpType.Operator = WhereParameter.Operand.NotEqual ;
            kpTable.Query.AddOrderBy(vw_Subject_Table.ColumnNames.OrderID, WhereParameter.Dir.ASC);
            kpTable.Query.Load();
            //显示各评分表
            kpTable.Rewind();
            while (!kpTable.EOF)
            {
                tr = new TableRow();
                //显示评分表类型
                cate.LoadByPrimaryKey(kpTable.KpType);
                tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, cate.KP_Category_Name, sStyleCenter));
                //tr.Cells.Add(HelperTable.CreateCell(1,1,kpTable.KpName,sStyleCenter));

                se.Rewind();
                while (!se.EOF)
                {
                    VW_ZB_Object zObject = new VW_ZB_Object();
                    zObject.Where.SubID.Value = SubID;
                    zObject.Where.KpID.Value = kpTable.KpID;
                    zObject.Where.TenderID.Value = se.TenderID;
                    zObject.Query.Load();
                    tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, zObject.ObjectScore.ToString(), sStyleCenter));

                    se.MoveNext();
                }
                tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, (kpTable.Rate * 100) + "%", sStyleCenter));
                tb.Rows.Add(tr);

                tr = new TableRow();
                kpTable.MoveNext();
            }

            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "综合得分", sStyleCenter));
            se.Rewind();
            while (!se.EOF)
            {
                vw_Subject_Tender vw = new vw_Subject_Tender();
                vw.Where.SubID.Value = SubID;
                vw.Where.TenderID.Value = se.TenderID;
                vw.Query.Load();
                tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, vw.Total.ToString(), sStyleCenter));

                se.MoveNext();
            }
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "", sStyleCenter));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "排名", sStyleCenter));
            se.Rewind();
            while (!se.EOF)
            {
                vw_Subject_Tender vw = new vw_Subject_Tender();
                vw.Where.SubID.Value = SubID;
                vw.Where.TenderID.Value = se.TenderID;
                vw.Query.Load();
                tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, vw.Grade1.ToString(), sStyleCenter));

                se.MoveNext();
            }
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "", sStyleCenter));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 2 + TenderCount, "评标结果", sStyleCenter));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1, "推荐中标候选人", sStyleCenter));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 1 + TenderCount, labResult.Text, sLEFT));
            tb.Rows.Add(tr);

            VW_DISTINCT_UMP ump = new VW_DISTINCT_UMP();
            ump.Where.SubjectID.Value = SubID;
            ump.Query.Load();
            int umpCount = ump.RowCount;
            
            tr= new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(umpCount + 1, 1, "评委签字", sStyleCenter));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 2, "姓名", sStyleCenter));
            tr.Cells.Add(HelperTable.CreateStyleCell(1, TenderCount - 1, @"单位/部门", sStyleCenter));
            tb.Rows.Add(tr);

            for (int i=1;i<=umpCount;i++)
            {
                tr= new TableRow();
                tr.Attributes.Add("Height", "32");
                tr.Cells.Add(HelperTable.CreateStyleCell(1, 2, "", sStyleCenter));
                tr.Cells.Add(HelperTable.CreateStyleCell(1, TenderCount - 1, @"", sStyleCenter));
                tb.Rows.Add(tr);
            }

            tr= new TableRow();

            Table tb1 = new Table();
            tb1.Attributes.Add("border","0");
            tb1.Attributes.Add("width", "100%");
            tb1.Attributes.Add("cellpadding","0");
            tb1.Attributes.Add("cellspacing","0");
            tb1.Attributes.Add("style","border-collapse:collapse");
            
            TableRow tr1 = new TableRow();
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"运维部门<br />领导签字", sStyleCenter));
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"", sStyleCenter));
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"需求部门<br />领导签字", sStyleCenter));
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"", sStyleCenter));
            tb1.Rows.Add(tr1);

            tr1 = new TableRow();
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"采购部门<br />领导签字", sStyleCenter));
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"", sStyleCenter));
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"采购管理部门<br />领导签字", sStyleCenter));
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"", sStyleCenter));
            tb1.Rows.Add(tr1);

            tr1 = new TableRow();
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"主管副总<br />审批意见", sStyleCenter));
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"", sStyleCenter));
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"总经理审<br />审批意见", sStyleCenter));
            tr1.Cells.Add(HelperTable.CreateStyleCell(1, 1, @"", sStyleCenter));
            tb1.Rows.Add(tr1);

            tr1 = new TableRow();
            tr1.Attributes.Add("height", "64");
            TableCell tc1 = HelperTable.CreateStyleCell(1, 1, @"", sStyleCenter);
            tc1.Attributes.Add("width", "25%");
            tr1.Cells.Add(tc1);
            tc1 = HelperTable.CreateStyleCell(1, 1, @"", sStyleCenter);
            tc1.Attributes.Add("width", "25%");
            tr1.Cells.Add(tc1);
            tc1 = HelperTable.CreateStyleCell(1, 1, @"", sStyleCenter);
            tc1.Attributes.Add("width", "25%");
            tr1.Cells.Add(tc1);
            tc1 = HelperTable.CreateStyleCell(1, 1, @"", sStyleCenter);
            tc1.Attributes.Add("width", "25%");
            tr1.Cells.Add(tc1);

            tb1.Rows.Add(tr1);

            tc = HelperTable.CreateCell(1, 2 + TenderCount, "");
            tc.Controls.Add(tb1);
            tr.Cells.Add(tc);

            tb.Rows.Add(tr);

            p1.Controls.Add(tb);
        }

        private void CreateResult(int SubID)
        {
            Table tb = new Table();
            tb.Attributes.Add("border","0");
            tb.Attributes.Add("width", "100%");
            tb.Attributes.Add("cellpadding","0");
            tb.Attributes.Add("cellspacing","0");
            tb.Attributes.Add("style","border-collapse:collapse");

            TableRow tr = new TableRow();
            TableCell tc = new TableCell();

            tc = HelperTable.CreateStyleCell(1, 2 , "推 荐 中 标 结 果", "font-size: 18.0pt;font-weight: 700;font-style: normal;text-decoration: none;font-family: 黑体;text-align: center;vertical-align: middle;white-space: normal;");
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);

            KP_Subject subject = new KP_Subject();
            subject.LoadByPrimaryKey(SubID);

            
            tr= new TableRow();
            tr.Cells.Add(tc = HelperTable.CreateStyleCell(1, 1, "项目名称：" + subject.SubjectName,sLEFT));
            tr.Cells.Add(tc = HelperTable.CreateStyleCell(1, 1, "项目编号：" + subject.Code, sLEFT));
            tb.Rows.Add(tr);

            
            tr= new TableRow();
            tr.Cells.Add(tc = HelperTable.CreateStyleCell(1, 1, "评标时间：" + DateTime.Now.ToShortDateString() , sLEFT));
            tr.Cells.Add(tc = HelperTable.CreateStyleCell(1, 1, "评标地点：" + subject.Address, sLEFT));
            tb.Rows.Add(tr);

            Table tb1 = new Table();
            tb1.Attributes.Add("border","0");
            tb1.Attributes.Add("width", "100%");
            tb1.Attributes.Add("cellpadding","0");
            tb1.Attributes.Add("cellspacing","0");
            tb1.Attributes.Add("style","border-collapse:collapse");

            TableRow tr1 = new TableRow();
            TableCell tc1 = new TableCell();
            int i =1 ;
            tc1 = HelperTable.CreateStyleCell(1, 1, "综合排名", sStyleCenter);
            tc1.Width = Unit.Pixel(160);
            tr1.Cells.Add(tc1);
            tc1 = HelperTable.CreateStyleCell(1, 1, "推 荐 中 标 候 选 人", sStyleCenter);
            tr1.Cells.Add(tc1);
            tb1.Rows.Add(tr1);

            vw_Subject_Tender tender = new vw_Subject_Tender();
            tender.Where.SubID.Value = SubID;
            tender.Query.AddOrderBy(vw_Subject_Tender.ColumnNames.Grade1, WhereParameter.Dir.ASC);
            tender.Query.Load();
            while (!tender.EOF)
            {
                ZB_Result zResult = new ZB_Result();
                if (zResult.LoadByPrimaryKey(SubID,tender.TenderID))
                {
                    tr1 = new TableRow();
                    tc1 = HelperTable.CreateStyleCell(1, 1, tender.Grade1.ToString(), sStyleCenter);
                    tr1.Cells.Add(tc1);

                    ZB_Tender t = new ZB_Tender();
                    t.LoadByPrimaryKey(tender.TenderID);
                    tc1 = HelperTable.CreateStyleCell(1, 1, t.FullName, sStyleLeft);

                    tr1.Cells.Add(tc1);
                    tb1.Rows.Add(tr1);
                }
                tender.MoveNext();
            }
            
            for (i=1;i<=3 ;i++) 
            {
                tr1 = new TableRow();
                tr1.Height =Unit.Pixel(28);
                tc1 = HelperTable.CreateStyleCell(1, 1, "", sStyleCenter);
                tr1.Cells.Add(tc1);
                tc1 = HelperTable.CreateStyleCell(1, 1, "", sStyleCenter);
                tr1.Cells.Add(tc1);
                tb1.Rows.Add(tr1);
            }

            tc = HelperTable.CreateCell(1,2,"");
            tc.Controls.Add(tb1);
            tr = new TableRow();
            tr.Cells.Add(tc);
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 2, "评委：", sLEFT));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tr.Cells.Add(HelperTable.CreateStyleCell(1, 2, "监标：", sLEFT));
            tb.Rows.Add(tr);

            tr = new TableRow();
            tc = HelperTable.CreateStyleCell(1, 2, "湖北信通通信有限公司惠达招标分公司", sRIGHT);
            tc.HorizontalAlign = HorizontalAlign.Right;
            tr.Cells.Add( tc);
            tb.Rows.Add(tr);

            p1.Controls.Add(tb);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            int SubID = HelperURL.GetIntFromUrlString("Subid");
            int TypeID = HelperURL.GetIntFromUrlString("Typeid");
            ZB_Result result = new ZB_Result();
            result.Where.SubjectID.Value = SubID;
            result.Where.TenderID.Value = HelperDigit.ConvertToInt32(dplTender.SelectedValue);
            if (!result.Query.Load())
            {
                result.AddNew();
                result.SubjectID = SubID;
                result.TenderID = HelperDigit.ConvertToInt32(dplTender.SelectedValue);

                vw_Subject_Table kpTable = new vw_Subject_Table();
                kpTable.Where.SubjectID.Value = SubID;
                kpTable.Where.KpType.Value = 3;//价格表
                kpTable.Query.Load();
                VW_ZB_ItemResult priceItem = new VW_ZB_ItemResult();
                priceItem.Where.SubID.Value = SubID;
                priceItem.Where.KpID.Value = kpTable.KpID;
                priceItem.Where.Is_Group.Value = false; //指标项
                priceItem.Where.Item_Type.Value = 0;    //第一个数值分
                priceItem.Where.TenderID.Value = result.TenderID;   //当前竞标人
                if (priceItem.Query.Load()) 
                {
                    //result.Price = priceItem.ItemScore;
                    //result.Remark = priceItem.ItemText;
                    result.Price = HelperCurrency.GetCurrencyDecimal("0" + priceItem.ItemScore);
                    result.Remark = "";
                }

                result.Save();
            }

            string URL = string.Format("SubjectResult.aspx?Subid={0}&TypeID={1}", SubID, TypeID);
            HelperURL.Redirect(URL);
        }



        protected void btnClear_Click(object sender, EventArgs e)
        {
            int SubID = HelperURL.GetIntFromUrlString("Subid");
            int TypeID = HelperURL.GetIntFromUrlString("Typeid");
            ZB_Result result = new ZB_Result();
            result.Where.SubjectID.Value = SubID;
            result.Query.Load();
            result.DeleteAll();
            result.Save();

            string URL = string.Format("SubjectResult.aspx?Subid={0}&TypeID={1}",SubID,TypeID);
            HelperURL.Redirect(URL);
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            int SubID = HelperURL.GetIntFromUrlString("Subid");
            //int TypeID = HelperURL.GetIntFromUrlString("Typeid");
            string URL = string.Format("../Appraisal/SubjectDisplay.aspx?id={0}", SubID);
            HelperURL.Redirect(URL);
        }

        protected void btnSubject_Click(object sender, EventArgs e)
        {
            int nSubjectID = HelperURL.GetIntFromUrlString("SubID");
            string sURL = string.Format("../ZB/SubjectResult.aspx?SubID={0}&TypeID=-1", nSubjectID);
            HelperURL.Redirect(sURL);
        }

        protected void btnResult_Click(object sender, EventArgs e)
        {
            int nSubjectID = HelperURL.GetIntFromUrlString("Subid");
            string sURL = string.Format("../ZB/SubjectResult.aspx?SubID={0}&TypeID=1", nSubjectID);
            HelperURL.Redirect(sURL);
        }
    
    }
}
